package com.tool4j.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tool4j.entity.AiSessionLog;
import com.tool4j.entity.params.ChatParams;
import com.tool4j.entity.params.PageParams;
import com.tool4j.entity.vo.DMessage;
import com.tool4j.service.ChatService;
import com.tool4j.util.UserPermission;
import com.tool4j.util.http.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;

import java.util.List;

@RestController
@RequestMapping("/api/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    @PostMapping(value = "/stream")
    public void chatStream(ServerWebExchange exchange, @RequestBody ChatParams params) {
        Long userId = UserPermission.get(exchange).getUserId();
        params.setUserId(userId);
        chatService.chatStream(params);
    }

    @PostMapping(value = "/findSessionPage")
    public Result<Page<AiSessionLog>> findSessionPage(ServerWebExchange exchange, @RequestBody PageParams<AiSessionLog> params) {
        Page<AiSessionLog> sessions = chatService.getSessions(exchange, params);
        return Result.ok(sessions);
    }

    @DeleteMapping(value = "/deleteSession/{id}")
    public Result<Void> deleteSession(ServerWebExchange exchange, @PathVariable("id") Long id) {
        chatService.deleteSession(exchange, id);
        return Result.ok();
    }

    @DeleteMapping(value = "/deleteAllSession")
    public Result<Void> deleteAllSession(ServerWebExchange exchange) {
        chatService.deleteAllSession(exchange);
        return Result.ok();
    }

    @GetMapping(value = "/getMessages/{id}")
    public Result<List<DMessage>> getMessages(@PathVariable("id") Long id) {
        List<DMessage> messages = chatService.getMessages(id);
        return Result.ok(messages);
    }
}
